<?php

/**
 * App filter form base class.
 *
 * @package    mydream
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BaseAppFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'name'                        => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'description'                 => new sfWidgetFormFilterInput(array('with_empty' => false)),
      'points_list'                 => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Point')),
      'features_point_history_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Feature')),
      'users_list'                  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'User')),
      'features_app_feature_list'   => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Feature')),
    ));

    $this->setValidators(array(
      'name'                        => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'description'                 => new sfValidatorPass(array('required' => false)),
      'points_list'                 => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Point', 'required' => false)),
      'features_point_history_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Feature', 'required' => false)),
      'users_list'                  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'User', 'required' => false)),
      'features_app_feature_list'   => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Feature', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('app_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addPointsListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.PointHistory PointHistory')
      ->andWhereIn('PointHistory.point_id', $values)
    ;
  }

  public function addFeaturesPointHistoryListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.PointHistory PointHistory')
      ->andWhereIn('PointHistory.feature_id', $values)
    ;
  }

  public function addUsersListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.UserApp UserApp')
      ->andWhereIn('UserApp.user_id', $values)
    ;
  }

  public function addFeaturesAppFeatureListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.AppFeature AppFeature')
      ->andWhereIn('AppFeature.feature_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'App';
  }

  public function getFields()
  {
    return array(
      'id'                          => 'Number',
      'name'                        => 'Number',
      'description'                 => 'Text',
      'points_list'                 => 'ManyKey',
      'features_point_history_list' => 'ManyKey',
      'users_list'                  => 'ManyKey',
      'features_app_feature_list'   => 'ManyKey',
    );
  }
}
